package com.zzb.projectfactory.controller.admin;

import com.zzb.projectfactory.framework.common.BaseController;
import com.zzb.projectfactory.core.loginhistory.entity.LoginHistory;
import com.zzb.projectfactory.framework.role.entity.User;
import com.zzb.projectfactory.core.loginhistory.form.LoginHistorySearchForm;
import com.zzb.projectfactory.core.loginhistory.service.LoginHistoryService;
import com.zzb.projectfactory.framework.common.BasePage;
import com.zzb.projectfactory.framework.common.BaseResponse;
import com.zzb.projectfactory.framework.common.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
* 登录历史controller
*/
@Controller
@RequestMapping("/admin/loginHistory")
public class AdminLoginHistoryController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(AdminLoginHistoryController.class);
    @Autowired
    private LoginHistoryService loginHistoryService;

    /**登录历史管理首页*/
    @RequestMapping({"","/","/index"})
    public String index(){
        return "admin/loginHistory/index";
    }
    /**登录历史列表json*/
    @RequestMapping("list")
    @ResponseBody
    public BasePage<LoginHistory> list(LoginHistorySearchForm loginHistorySearchForm){
        return loginHistoryService.queryPage(loginHistorySearchForm, LoginHistory.class);
    }

    /**登录历史表单页面*/
    @RequestMapping("form")
    public String form(String id, Model model){
        LoginHistory loginHistory;
        if(StringUtils.isEmpty(id)){
            loginHistory = new LoginHistory();
    } else{
        loginHistory = loginHistoryService.get(id);
        if(loginHistory == null) loginHistory = new LoginHistory();
    }
        model.addAttribute("loginHistory",loginHistory);
        return "admin/loginHistory/form";
    }

    /**登录历史保存*/
    @RequestMapping("/save")
    @ResponseBody
    public BaseResponse<LoginHistory> save(LoginHistory loginHistory , HttpServletRequest request){
        try {
            User user = getCurrentUser(request);
            return loginHistoryService.saveLoginHistory(loginHistory, user);
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse<>(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**登录历史删除*/
    @RequestMapping("/delete")
    @ResponseBody
    public BaseResponse delete(String id,HttpServletRequest request){
        try {
            return loginHistoryService.deleteLoginHistory(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**登录历史开启*/
    @RequestMapping("/open")
    @ResponseBody
    public BaseResponse open(String id ,HttpServletRequest request){
        try {
            return loginHistoryService.recovery(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**登录历史屏蔽*/
    @RequestMapping("/deny")
    @ResponseBody
    public BaseResponse deny(String id ,HttpServletRequest request){
        try {
            return loginHistoryService.trash(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

}
